package com.jeesit.cloud.spot.business.impl.Impl;

import com.jeesit.cloud.dto.*;
import com.jeesit.cloud.model.*;
import com.jeesit.cloud.spot.business.impl.ITitleBiz;
import com.jeesit.cloud.spot.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class TitleBizImpl implements ITitleBiz {

    @Autowired
    private ITitleService titleService;
    @Autowired
    private ITitleInvoiceService titleInvoiceService;
    @Autowired
    private ITitleCommerceService titleCommerceService;
    @Autowired
    private ITitleAccountService titleAccountService;
    @Autowired
    private ITitleLinkmanService titleLinkmanService;

    /**
     * 添加我方/客户抬头
     * @param user
     * @param id
     * @param entity
     * @return
     */
    @Override
    @Transactional
    public JSONResult<Title> saveData(User user, Long id, TitleDto entity) {
        TitleDto titleDto = new TitleDto();
        titleDto.setCodes(entity.getCode());
        List<Title> titles = titleService.list(1, titleDto);
        Title it = new Title();
        if(id==null){
            Assert.isTrue(titles.size()==0,"社会信用代码已存在，请核对后重新输入");
            it.setAddTime(new Date());
            it.setAddUserUuid(user.getId());
            it.setAddUserName(user.getNickName());
            it.setDeleted(false);
        }else {
            Assert.isTrue(titles.size()==0||titles.get(0).getUuid().equals(id),"社会信用代码已存在，请核对后重新输入");
            it=titleService.getById(id);
            it.setModTime(new Date());
            it.setModUserUuid(user.getId());
            it.setModUserName(user.getNickName());
        }
        it.setPropertyClient(entity.getPropertyClient());
        it.setPropertySupply(entity.getPropertySupply());
        it.setShortName(entity.getShortName());
        it.setFullName(entity.getFullName());
        it.setCode(entity.getCode());
        it.setCompanyType(entity.getCompanyType());
        it.setParentUuid(entity.getParentUuid());
        it.setOrgUuid(entity.getOrgUuid());
        it.setBizOrgUuid(entity.getBizOrgUuid());
        it.setBizOrgName(entity.getBizOrgName());
        it.setParentName(entity.getParentName());
        it.setPath(entity.getPath());
        it.setRemark(entity.getRemark());
        titleService.saveOrUpdate(it);
        String treePath = "0,";
        if (it.getParentUuid() > 0) {
            Title title = titleService.getById(it.getParentUuid());
            treePath = title.getTreePath();
        }
        treePath += it.getUuid() + ",";
        it.setTreePath(treePath);
        titleService.saveOrUpdate(it);
        List<TitleAccount> titleAccounts = new ArrayList<>();
        if(entity.getTitleAccountDtos()!=null&&entity.getTitleAccountDtos().size()>0){//账号信息
            if(id!=null){//编辑前先删除之前数据
                titleAccountService.deltedDate(id);
            }
            List<TitleAccountDto> titleAccountDtos = entity.getTitleAccountDtos();
            for(TitleAccountDto its : titleAccountDtos){
                TitleAccount titleAccount = new TitleAccount();
                titleAccount.setType(its.getType());
                titleAccount.setAccount(its.getAccount());
                titleAccount.setAccountName(its.getAccountName());
                titleAccount.setBranchName(its.getBranchName());
                titleAccount.setCurrency(its.getCurrency());
                titleAccount.setBranchNo(its.getBranchNo());
                titleAccount.setIsInvoice(its.getIsInvoice());
                titleAccount.setIsUse(its.getIsUse());
                titleAccount.setTitleUuid(it.getUuid());
                titleAccount.setAddTime(new Date());
                titleAccount.setAddUserName(user.getNickName());
                titleAccount.setAddUserUuid(user.getId());
                titleAccount.setDeleted(false);
                titleAccounts.add(titleAccount);
            }
            titleAccountService.saveOrUpdateBatch(titleAccounts);
        }
        if(entity.getTitleCommerceDto()!=null){//工商信息
            if(id!=null){//编辑前先删除之前数据
                titleCommerceService.deltedDate(id);
            }
            TitleCommerceDto titleCommerceDto = entity.getTitleCommerceDto();
            TitleCommerce titleCommerce = new TitleCommerce();
            titleCommerce.setFullName(titleCommerceDto.getFullName());
            titleCommerce.setCode(titleCommerceDto.getCode());
            titleCommerce.setRegisteredAddress(titleCommerceDto.getRegisteredAddress());
            titleCommerce.setLegalPerson(titleCommerceDto.getLegalPerson());
            titleCommerce.setCertificateType(titleCommerceDto.getCertificateType());
            titleCommerce.setCertificateOn(titleCommerceDto.getCertificateOn());
            titleCommerce.setLoseDate(titleCommerceDto.getLoseDate());
            titleCommerce.setCommercePhone(titleCommerceDto.getCommercePhone());
            titleCommerce.setFax(titleCommerceDto.getFax());
            titleCommerce.setType(titleCommerceDto.getType());
            titleCommerce.setEstDate(titleCommerceDto.getEstDate());
            titleCommerce.setBusDate(titleCommerceDto.getBusDate());
            titleCommerce.setRegisteredCapital(titleCommerceDto.getRegisteredCapital());
            titleCommerce.setContributedCapital(titleCommerce.getRegisteredCapital());
            titleCommerce.setTitleUuid(it.getUuid());
            titleCommerce.setAddTime(new Date());
            titleCommerce.setAddUserName(user.getNickName());
            titleCommerce.setAddUserUuid(user.getId());
            titleCommerce.setDeleted(false);
            titleCommerceService.saveOrUpdate(titleCommerce);
        }
        if(entity.getTitleInvoiceDto()!=null){//开票信息
            if(id!=null){//编辑前先删除之前数据
                titleInvoiceService.deltedDate(id);
            }
            TitleInvoiceDto titleInvoiceDto = entity.getTitleInvoiceDto();
            TitleInvoice titleInvoice = new TitleInvoice();
            titleInvoice.setFullName(titleInvoiceDto.getFullName());
            titleInvoice.setRegistrationNumber(titleInvoiceDto.getRegistrationNumber());
            titleInvoice.setInvoicePhone(titleInvoiceDto.getInvoicePhone());
            titleInvoice.setInvoiceAddress(titleInvoiceDto.getInvoiceAddress());
            titleInvoice.setRecipients(titleInvoiceDto.getRecipients());
            titleInvoice.setRecipientsAddress(titleInvoiceDto.getRecipientsAddress());
            titleInvoice.setRecipientsPhone(titleInvoiceDto.getRecipientsPhone());
            titleInvoice.setPostalCode(titleInvoiceDto.getPostalCode());
            titleInvoice.setTitleUuid(it.getUuid());
            titleInvoice.setAddTime(new Date());
            titleInvoice.setAddUserName(user.getNickName());
            titleInvoice.setAddUserUuid(user.getId());
            titleInvoice.setDeleted(false);
            titleInvoiceService.saveOrUpdate(titleInvoice);
        }
        if(entity.getTitleLinkmanDtos()!=null&&entity.getTitleLinkmanDtos().size()>0){//联系人信息
            if(id!=null){//编辑前先删除之前数据
                titleLinkmanService.deltedDate(id);
            }
            List<TitleLinkman> titleLinkmanList = new ArrayList<>();
            List<TitleLinkmanDto> titleLinkmanDtos = entity.getTitleLinkmanDtos();
            for(TitleLinkmanDto its : titleLinkmanDtos){
                TitleLinkman titleLinkman = new TitleLinkman();
                titleLinkman.setLinkman(its.getLinkman());
                titleLinkman.setLinkmanType(its.getLinkmanType());
                titleLinkman.setJob(its.getJob());
                titleLinkman.setBusiness(its.getBusiness());
                titleLinkman.setOfficePhone(its.getOfficePhone());
                titleLinkman.setMobileTelephone(its.getMobileTelephone());
                titleLinkman.setQq(its.getQq());
                titleLinkman.setWechat(its.getWechat());
                titleLinkman.setMailbox(its.getMailbox());
                titleLinkman.setFax(its.getFax());
                titleLinkman.setRemark(its.getRemark());
                titleLinkman.setTitleUuid(it.getUuid());
                titleLinkman.setAddTime(new Date());
                titleLinkman.setAddUserName(user.getNickName());
                titleLinkman.setAddUserUuid(user.getId());
                titleLinkman.setDeleted(false);
                titleLinkmanList.add(titleLinkman);
            }
            titleLinkmanService.saveOrUpdateBatch(titleLinkmanList);
        }
        JSONResult<Title> res = new JSONResult<>();
        res.setData(it);
        res.setMessage("操做成功");
        res.setSuccess(true);
        return res;
    }
}
